package com.ccys.cms.service.discount.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.discount.DeeplinkService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.discount.DeeplinkDto;
import com.ccys.db.entity.TblClinic;
import com.ccys.db.entity.TblDeeplink;
import com.ccys.db.mapper.TblDeeplinkMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.discount.DeeplinkPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 深度链接接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-18 15:51
 **/

@Service
public class DeeplinkServiceImpl extends BaseServiceImpl<TblDeeplink> implements DeeplinkService {

    @Resource
    private TblDeeplinkMapper tblDeeplinkMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDeeplink(DeeplinkDto dto) {
        //校验字段值
        Kit.allParamValid(dto, "name", "countryId", "mobilePlatform", "deeplinkIdentifier", "consultFeeNormalHours"
                , "consultFeeAfterHours", "status", "sessionTimeout", "allowPromoCode", "timeoutWarning");
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDeeplinkStatus(DeeplinkDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblDeeplink tblDeeplink = new TblDeeplink();
        tblDeeplink.setId(dto.getId());
        tblDeeplink.setStatus(dto.getStatus());
        if (tblDeeplinkMapper.updateByPrimaryKeySelective(tblDeeplink) > 0) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<DeeplinkPageQueryVo>, Object> findPageDeeplink(DeeplinkDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        dto.setMobilePlatform(Optional.ofNullable(dto.getMobilePlatform()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DeeplinkPageQueryVo> list = tblDeeplinkMapper.findPageDeeplink(dto);
        PageInfo<DeeplinkPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDeeplink(DeeplinkDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblDeeplink tblDeeplink = new TblDeeplink();
        tblDeeplink.setId(dto.getId());
        tblDeeplink.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblDeeplinkMapper.updateByPrimaryKeySelective(tblDeeplink) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }
}
